package com.ndp.fb.walle.business.impl;

import com.ndp.fb.exception.BusinessException;
import com.ndp.fb.model.business.vo.cache.result.PublishCache;
import com.ndp.fb.redis.api.RedisClient;
import com.ndp.fb.walle.business.CacheService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Created by infi on 2015/1/29.
 */
@Component
public class CacheServiceImpl implements CacheService {
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RedisClient redisClient;

    @Override
    public void cache(String key, PublishCache cache, long timeout, TimeUnit unit) throws BusinessException {
        redisClient.set(key, cache, timeout, unit.name());
    }

    @Override
    public void update(String key, PublishCache cache) throws BusinessException {
        redisClient.set(key, cache);
    }

    @Override
    public PublishCache fetch(String key) throws BusinessException {
        return (PublishCache) redisClient.get(key);
    }

    @Override
    public void delete(String key, PublishCache cache) {
        redisClient.delete(key);
    }

    @Override
    public Object fetchObject(String key) throws BusinessException {
        return redisClient.get(key);
    }

}